package wuaipojie

import (
	_ "embed"
	"gitee.com/kaylee595/gkaylee/kerr"
	"github.com/robertkrimen/otto"
	"sync"
)

//go:embed waf.js
var wafJS string
var wafJSInitMutex sync.Mutex
var wafJSContext *otto.Otto

func (e *ErrNeedVerifyWaf) GeneratePayload() (string, error) {
	if wafJSContext == nil {
		wafJSInitMutex.Lock()
		if wafJSContext == nil {
			var err error
			wafJSContext, _, err = otto.Run(wafJS)
			if err != nil {
				wafJSInitMutex.Unlock()
				return "", kerr.WrapTraceStack(err)
			}
		}
		wafJSInitMutex.Unlock()
	}
	ret, err := wafJSContext.Call("GeneratePayload", nil, e.LZ, e.LJ, e.LE, e.UserAgent)
	if err != nil {
		return "", kerr.WrapTraceStack(err)
	}
	return ret.ToString()
}
